// function shallowCopy(obj) {
//   if (typeof obj !== 'object') return
//   let copyObj = Array.isArray(obj) ? [] : {}
//   for (let key in obj) {
//     if (obj.hasOwnProperty(key)) {
//       copyObj[key] = obj[key]
//     }
//   }
//   return copyObj
// }

function isObject(obj) {
  return typeof obj === 'object' && obj !== null
}
function deepClone(obj, map = new WeakMap()) {
  if (map.get(obj)) {
    return obj
  }
  if (isObject(obj)) {
    map.set(obj, true)
    let copyObj = Array.isArray(obj) ? [] : {}
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        copyObj[key] = deepClone(obj[key], map)
      }
    }
    return copyObj
  } else {
    return obj
  }
}

const obj =  {
  a: 1,
  b: {
    c: 2
  }
}
console.log(deepClone(obj))